package cn.lyjuan.first.hadoop.util;

import cn.lyjuan.base.util.RandomUtils;
import cn.lyjuan.base.util.StringUtils;
import cn.lyjuan.first.hadoop.demo.ch04.Ch04S01WordCount;
import cn.lyjuan.first.hadoop.demo.enums.ChNameEnum;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;

import java.io.*;

public class FileUtil {
    /**
     * 获取项目根目录
     *
     * @return
     */
    public static String projectHome() {
        String path = System.getProperty("user.dir");
        return path;
    }

    public static String docPath(ChNameEnum ch, Class cls, String fileName) {
        return projectHome() + File.separator + "doc" + File.separator + ch.name() + File.separator
                + cls.getSimpleName() + File.separator + fileName;
    }

//    /**
//     * 获取项目doc目录下的子目录
//     *
//     * @param subPath
//     * @return
//     */
//    public static String docPath(String subPath) {
//        if (StringUtils.isNull(subPath))
//            subPath = "";
//        else if (subPath.startsWith("/"))
//            return subPath;
//        else if (subPath.startsWith("./")) {
//            subPath = subPath.replace("./", "");
//        }
//        return projectHome() + File.separator + "doc" + File.separator + subPath;
//    }

    /**
     * 远程文件路径
     *
     * @param ch
     * @param cls
     * @param fileName 文件名
     * @return
     */
    public static String remotePath(ChNameEnum ch, Class cls, String fileName) {
        String path = remotePath(ch, cls);
        return path + "/" + fileName;
    }

    /**
     * 获取测试章节demo目录
     *
     * @param ch
     * @return
     */
    public static String remotePath(ChNameEnum ch, Class cls) {
        return "/hadoop/demo/" + ch.name() + "/" + cls.getSimpleName();
    }

    /**
     * 远程全URI地址
     *
     * @param ch
     * @param cls
     * @return
     */
    public static Path remoteURIPath(ChNameEnum ch, Class cls, String subPath) {
        return new Path(HDFSUtil.HADOOP_URI + remotePath(ch, cls, subPath));
    }
}
